/*
 * Copyright (C), 2002-2016, 苏宁易购电子商务有限公司
 * FileName: ModelProbHandleController.java
 * Author:   13075787
 * Date:     2016年7月11日 下午12:04:14
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.web.controller.model;

import java.security.Principal;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.model.ModelConstant;
import com.suning.sawp.intf.model.ModelProbOprReqBean;
import com.suning.sawp.intf.model.ModelProbOprReqList;
import com.suning.sawp.intf.model.ModelProbReason;
import com.suning.sawp.intf.model.ModelProbVo;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.service.impl.model.ModelBlendServiceImpl;
import com.suning.sawp.service.impl.model.ModelProbServiceImpl;
import com.suning.sawp.web.controller.DjBaseController;
import com.suning.sawp.web.exception.MyGsonView;

/**
 * 样机管理<br>
 * 异常处理和审核
 *
 * @author 13075787
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/model/probHandle")
public class ModelProbHandleController extends DjBaseController {
    /**
     * 单商品样机总数最大值
     */
    private static final int MODEL_MAX = 99;
    
    @Autowired
    ModelBlendServiceImpl modelBlendService;

    @Autowired
    StoreManService storeManService;

    @Autowired
    ModelProbServiceImpl modelProbService;

    /**
     * 功能描述: <br>
     * 查询样机异常原因列表
     */
    @RequestMapping("/queryProbReasons.do")
    public MyGsonView queryModelProbReasons(Principal principal) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<List<ModelProbReason>> ret = modelProbService.queryModelProbReason();
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 查询未处理的异常列表
     */
    @RequestMapping("/queryProbsNoHandle.do")
    public MyGsonView queryProbNoHandleList(Principal principal, Integer index, Integer size) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<List<ModelProbVo>> ret = modelProbService.queryModelProbNoHandleList(storeMan, index, size);
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 查询未审核的异常列表
     */
    @RequestMapping("/queryProbsNoAudit.do")
    public MyGsonView queryProbNoAuditList(Principal principal, Integer index, Integer size) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<List<ModelProbVo>> ret = modelProbService.queryModelProbNoAuditList(storeMan, index, size);
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 异常处理
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping("/probHandle.do")
    public MyGsonView modelProbHandle(Principal principal, @RequestBody ModelProbOprReqList modelProbOprReqList) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan) && null != modelProbOprReqList) {
            // 校验样机数量不能大于99
            if (!checkCommNum(modelProbOprReqList.getProbOprList())) {
                setErrorMsg(view, ErrorCodeConstants.ERR_0037);
            } else {
                ReturnMsg ret = modelProbService.modelProbHandle(storeMan, ModelConstant.PROBLEM_OPR_STATUS_DONE,
                        modelProbOprReqList.getProbOprList());
                view = setGsonView(ret);
            }
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }
    
    /**
     * 如果维护样机数量，则不能大于99
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param list
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private boolean checkCommNum(List<ModelProbOprReqBean> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            for (ModelProbOprReqBean bean : list) {
                if (null == bean.getCommNum() || bean.getCommNum() > MODEL_MAX || bean.getCommNum() < 0) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 功能描述: <br>
     * 异常审核
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping("/probAudit.do")
    public MyGsonView modelProbAudit(Principal principal, Integer status,
            @RequestBody ModelProbOprReqList modelProbOprReqList) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan) && null != modelProbOprReqList) {
            if (null != status) {
                // 校验样机数量不能大于99
                if (!checkCommNum(modelProbOprReqList.getProbOprList())) {
                    setErrorMsg(view, ErrorCodeConstants.ERR_0037);
                    return view;
                }
                if (status.equals(ModelConstant.AUDIT_PASS)) {
                    // 审核通过
                    ReturnMsg ret = modelProbService.modelProbHandle(storeMan, ModelConstant.PROBLEM_OPR_STATUS_OVER,
                            modelProbOprReqList.getProbOprList());
                    view = setGsonView(ret);
                } else {
                    // 审核驳回
                    ReturnMsg ret = modelProbService.modelProbHandle(storeMan, ModelConstant.PROBLEM_OPR_STATUS_ING,
                            modelProbOprReqList.getProbOprList());
                    view = setGsonView(ret);
                }
            } else {
                ReturnMsg ret = new ReturnMsg();
                ret.setError(ModelConstant.MODEL_ERROR_NO_PARAM,
                        ModelConstant.getErrorValue(ModelConstant.MODEL_ERROR_NO_PARAM));
                view = setGsonView(ret);
            }
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

}
